package com.emm.yixun.mobile.ui.calculateprice;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.emm.yixun.mobile.model.GetCalculatePrice;
import com.emm.yixun.mobile.model.InfoDest;
import com.emm.yixun.mobile.ui.selectcustomers.SelectcustomersActivity;
import com.eroad.product.tools.PickerView;
import com.eroad.product.tools.PickerView.onSelectListener;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import xf.tools.Loading;

/**
 * 
 * <p>
 * <b>Title：</b>算价开始界面
 * </p>
 * <p>
 * <b>Description：</b>
 * </p>
 * 
 * @author tangxin
 * @date 2016年12月6日 下午4:11:22
 */
public class CalculatePriceActivity extends BaseActivity {

	private static final String TAG = "CalculatePriceActivity";

	/** 总价 */
	private String totalAmount_ = "0";
	/** 房号ID */
	private String roomId = "";

	public static String FirstPrice = "";

	/** 商贷利率 */
	public static String businessLoanRate_price = "";

	/** 公积金利率 */
	public static String fundLoanRate_price = "";

	/** 贷款利率类型 */
	public static String loanRate_name = "";

	/** 倍率 */
	public static String Rate = "";

	/** title名称 */
	TextView pr_name;

	/** 房源总价 */
	EditText housesSumPrice_view;

	/** 首付 */
	EditText firstPay_view;

	/** 贷款类型 */
	EditText loanType_view;

	/** 1="公积金贷款" 2="商业贷款" 3="组合贷款" */
	public static int loantype_ = 3;

	/** 公积金贷款 */
	EditText fundLoan_view;

	/** 商业贷款 */
	EditText businessLoan_view;

	/** 贷款利率 */
	EditText loanRate_view;

	/** 商业贷款率 */
	EditText businessLoanRate_view;

	/** 公积金贷款率 */
	EditText fundLoanRate_view;

	/** 贷款年限 */
	EditText loanLife_view;

	/** 开始算价 */
	Button calculatePrice_view;

	/** 页面title */
	TextView title_main;

	/** back按钮 */
	ImageView back_btn;

	/* ---公用选择框--- */
	View view;
	PopupWindow popu;
	/** 选中内容 */
	String selectName;
	/** 选中下标 */
	int SelectID;
	ArrayList<String> listname;
	ArrayList<InfoDest> listname2;

	/** 打开客户选择界面 */
	ImageView open_select_activity;

	/** 客户名称 */
	EditText customerName_view;

	/** 客户电话 */
	EditText customerPhone_view;

	/** 是否提示客户关联 */
	boolean isPrompt = false;

	/** 按钮是否为打开状态 false 为打开 true为清空 */
	boolean isOpenBtn = false;

	/** 选择的客户ID */
	private String customerId = "";

	/** 侧滑关闭框架 */
	private SwipeBackLayout mSwipeBackLayout;

	/** 用于监听最外层布局高度，判断键盘弹出不展示保存和提交审核的底部菜单 */
	RelativeLayout background_key;

	/** 底部菜单布局 */
	LinearLayout buttom_select;

	/* ---公用选择框--- */

	LinearLayout fundLoan_p1, businessLoan_p1, fundLoan_p2, businessLoan_p2;

	private String pr_namea = "";

	@Override
	protected void onResume() {
		BaseActivity.activity = this;
		firstPay_view.setText(FirstPrice);
		businessLoanRate_view.setText(businessLoanRate_price);
		fundLoanRate_view.setText(fundLoanRate_price);
		loanRate_view.setText(loanRate_name);
		IsVsiBle();
		super.onResume();
	}

	@Override
	protected void onCreate(Bundle arg0) {
		IsVisibleStatusBar = false;
		super.onCreate(arg0);
		setContentView(R.layout.calculateprice_activity);

		// SetSwipeBackLayout(mSwipeBackLayout);

		BaseActivity.activity = this;

		loantype_ = 3;

		pr_namea = getIntent().getStringExtra("pr_name");
		totalAmount_ = getIntent().getStringExtra("totalAmount");

		if ("".equals(totalAmount_) || null == totalAmount_ || "null".equals(totalAmount_)
				|| TextUtils.isEmpty(totalAmount_)) {
			totalAmount_ = "0";
		}

		totalAmount_ = EmmApplication.drop2("" + (Double.valueOf(totalAmount_).doubleValue() * 10000));
		roomId = getIntent().getStringExtra("roomId");

		Log.v(TAG, "totalAmount_:" + totalAmount_ + "-/-roomId:" + roomId);

		initView();

		initBtn();

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		FirstPrice = null;
		businessLoanRate_price = null;
		fundLoanRate_price = null;
		loanRate_name = null;
		Rate = null;
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化view
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 上午11:43:53
	 */
	private void initView() {
		pr_name = (TextView) findViewById(R.id.pr_name);
		pr_name.setText(pr_namea);
		firstPay_view = (EditText) findViewById(R.id.firstPay_view);
		loanType_view = (EditText) findViewById(R.id.loanType_view);
		fundLoan_view = (EditText) findViewById(R.id.fundLoan_view);
		loanRate_view = (EditText) findViewById(R.id.loanRate_view);
		businessLoanRate_view = (EditText) findViewById(R.id.businessLoanRate_view);
		fundLoanRate_view = (EditText) findViewById(R.id.fundLoanRate_view);
		calculatePrice_view = (Button) findViewById(R.id.calculatePrice_view);
		title_main = (TextView) findViewById(R.id.title_main);
		title_main.setText(getString(R.string.ui_ppo));
		back_btn = (ImageView) findViewById(R.id.back_btn);
		loanLife_view = (EditText) findViewById(R.id.loanLife_view);
		fundLoan_p1 = (LinearLayout) findViewById(R.id.fundLoan_p1);
		businessLoan_p1 = (LinearLayout) findViewById(R.id.businessLoan_p1);
		fundLoan_p2 = (LinearLayout) findViewById(R.id.fundLoan_p2);
		businessLoan_p2 = (LinearLayout) findViewById(R.id.businessLoan_p2);
		businessLoan_view = (EditText) findViewById(R.id.businessLoan_view);
		housesSumPrice_view = (EditText) findViewById(R.id.housesSumPrice_view);
		housesSumPrice_view.setText(totalAmount_);
		open_select_activity = (ImageView) findViewById(R.id.open_select_activity);
		customerName_view = (EditText) findViewById(R.id.customerName_view);
		customerPhone_view = (EditText) findViewById(R.id.customerPhone_view);

		findViewById(R.id.statusbar_bg).setVisibility(View.GONE);
		// SetRelativiLayoutPadding(R.id.main_head_top_rela);

		background_key = (RelativeLayout) findViewById(R.id.calculate_background_key);
		buttom_select = (LinearLayout) findViewById(R.id.btn);
	}

	/**
	 * 回调方法
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.i(TAG, "onActivityResult" + "requestCode" + requestCode + "\n resultCode=" + resultCode);
		if (requestCode == 141) {
			if (resultCode == 141) {
				customerId = data.getStringExtra("customerId");
				String customerName = data.getStringExtra("customerName");
				String customerPhone = data.getStringExtra("customerPhone");
				Log.v(TAG, "customerId:" + customerId + "-----customerName:" + customerName + "-----customerPhone:"
						+ customerPhone);
				customerName_view.setText(customerName);
				customerName_view.setEnabled(false);
				customerPhone_view.setText(customerPhone);
				customerPhone_view.setEnabled(false);
				isPrompt = true;
				isOpenBtn = true;
				open_select_activity.setImageResource(R.drawable.icon_03);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);

	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化监听事件
	 * </p>
	 * <p>
	 * <b>Description：</b>2017年1月6日10:10:17 tangxin
	 * 修改:用于监听最外层布局高度，判断键盘弹出不展示保存和提交审核的底部菜单
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 上午11:44:12
	 */
	private void initBtn() {

		background_key.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			@Override
			public void onGlobalLayout() {
				// 比较Activity根布局与当前布局的大小
				int heightDiff = background_key.getRootView().getHeight() - background_key.getHeight();
				Log.v(TAG, "heightDiff:" + (heightDiff + statusBarHeight));
				if (heightDiff > (100 + statusBarHeight)) {// 此处因为是抹去状态栏高度，所以需要加一个状态栏高度
					// 大小超过100时，一般为显示虚拟键盘事件
					Log.v(TAG, "---------大于一百--键盘打开");
					buttom_select.setVisibility(View.GONE);
				} else {
					// 大小小于100时，为不显示虚拟键盘或虚拟键盘隐藏
					Log.v(TAG, "---------小于一百--键盘关闭");
					buttom_select.setVisibility(View.VISIBLE);
				}
			}
		});

		open_select_activity.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isOpenBtn) {// 清空
					isOpenBtn = false;
					open_select_activity.setImageResource(R.drawable.icon_05);
					isPrompt = false;
					customerName_view.setText("");
					customerName_view.setEnabled(true);
					customerPhone_view.setText("");
					customerPhone_view.setEnabled(true);
				} else {
					Intent intent = new Intent(CalculatePriceActivity.this, SelectcustomersActivity.class);
					startActivityForResult(intent, 141);
				}
			}
		});
		customerName_view.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (null == s || s.toString().length() <= 0) {
					if (EmmApplication.isNull(customerPhone_view.getText().toString().trim())) {
						isOpenBtn = true;
						open_select_activity.setImageResource(R.drawable.icon_03);
					} else {
						isOpenBtn = false;
						open_select_activity.setImageResource(R.drawable.icon_05);
					}
				} else {
					isOpenBtn = true;
					open_select_activity.setImageResource(R.drawable.icon_03);
				}
			}
		});

		customerPhone_view.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (null == s || s.toString().length() <= 0) {
					if (EmmApplication.isNull(customerName_view.getText().toString().trim())) {
						open_select_activity.setImageResource(R.drawable.icon_03);
						isOpenBtn = true;
					} else {
						isOpenBtn = false;
						open_select_activity.setImageResource(R.drawable.icon_05);
					}
				} else {
					open_select_activity.setImageResource(R.drawable.icon_03);
					isOpenBtn = true;
				}
			}
		});

		loanType_view.setText("组合贷款");

		calculatePrice_view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				EmmApplication.updateUrl("getCalculatePrice");
				Map<String, String> map = new HashMap<String, String>();
				if (EmmApplication.isNull(housesSumPrice_view.getText().toString().trim())) {
					if (Double.valueOf(housesSumPrice_view.getText().toString().trim()).doubleValue() > 0) {
						map.put("housesSumPrice", housesSumPrice_view.getText().toString().trim());
					} else {
						EmmApplication.Ts("房源总价不能为0");
						return;
					}
				} else {
					EmmApplication.Ts("房源总价不能为空");
					return;
				}

				if (EmmApplication.isNull(firstPay_view.getText().toString().trim())) {
					map.put("firstPay", firstPay_view.getText().toString().trim());
				} else {
					EmmApplication.Ts("首付不能为空");
					return;
				}

				int type = 1;

				if (TextUtils.isEmpty(loanType_view.getText().toString()) || null == loanType_view.getText().toString()) {
					EmmApplication.Ts("请选择贷款类型 ");
					return;
				} else {
					// 贷款类型：1、组合贷款；2、公积金贷款；3、商业贷款
					if ("公积金贷款".equals(loanType_view.getText().toString())) {
						type = 2;
						map.put("loanType", "2");
					} else if ("商业贷款".equals(loanType_view.getText().toString())) {
						type = 3;
						map.put("loanType", "3");
					} else if ("组合贷款".equals(loanType_view.getText().toString())) {
						type = 1;
						map.put("loanType", "1");
					}
				}

				if (EmmApplication.isNull(fundLoan_view.getText().toString().trim())) {
					map.put("fundLoan", fundLoan_view.getText().toString().trim());
				} else {
					if (type == 1 || type == 2) {
						EmmApplication.Ts("公积金贷款不能为空");
						return;
					}
				}

				if (EmmApplication.isNull(businessLoan_view.getText().toString().trim())) {
					map.put("businessLoan", businessLoan_view.getText().toString().trim());
				} else {
					if (type == 1 || type == 3) {
						EmmApplication.Ts("商业贷款不能为空");
						return;
					}
				}

				if (EmmApplication.isNull(loanRate_view.getText().toString().trim())) {
					map.put("loanRate", loanRate_view.getText().toString().trim());
				} else {
					EmmApplication.Ts("贷款利率不能为空");
					return;
				}

				if (EmmApplication.isNull(fundLoanRate_view.getText().toString().trim())) {
					if (type == 1 || type == 2)
						map.put("fundLoanRate", fundLoanRate_view.getText().toString().trim());
				} else {
					if (type == 1 || type == 2) {
						EmmApplication.Ts("公积金贷款率不能为空");
						return;
					}
				}

				if (EmmApplication.isNull(businessLoanRate_view.getText().toString().trim())) {
					if (type == 1 || type == 3)
						map.put("businessLoanRate", businessLoanRate_view.getText().toString().trim());
				} else {
					if (type == 1 || type == 3) {
						EmmApplication.Ts("商业贷款率不能为空");
						return;
					}
				}
				Log.v(TAG, "fidg:==>" + fidg);
				if (null == fidg || "".equals(fidg) || TextUtils.isEmpty(fidg)) {
					EmmApplication.Ts("请选择贷款年限");
					return;
				} else {
					map.put("loanLife", fidg);
				}
				map.put("version", EmmApplication.getPackageManager(5));
				map.put("merchantId", EmmApplication.getData(EmmApplication.merchantId));
				map.put("token", EmmApplication.getToken());
				map.put("userId", EmmApplication.getUserId());
				map.put("projectCode", EmmApplication.getProjectCode());
				map.put("projectId", EmmApplication.getProjectID());
				map.put("terminalCode", EmmApplication.getPackageManager(2));
				map.put("roomId", roomId);
				if (isPrompt) {// 打开提示框
					popu("是否将" + pr_namea + "标记为客户意向房源", map);
				} else {
					GetCalculatePrice(map);
				}
			}
		});
		back_btn.setVisibility(View.VISIBLE);
		back_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		firstPay_view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(CalculatePriceActivity.this, FirstPayActivity.class);
				intent.putExtra("totalAmount_", null == housesSumPrice_view.getText().toString().trim() ? "0"
						: housesSumPrice_view.getText().toString().trim());
				intent.putExtra("TYPE", "1");
				startActivity(intent);
			}
		});
		fundLoan_view.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (null != s && !"".equals(s.toString())) {
					if (null == totalAmount_ || Double.valueOf(totalAmount_).doubleValue() <= 0) {
						EmmApplication.T2("房源总价不能为0");
						fundLoan_view.setText("");
						businessLoan_view.setText("");
					} else {
						if (null == firstPay_view.getText().toString().trim()
								|| "".equals(firstPay_view.getText().toString().trim())
								|| Double.valueOf(firstPay_view.getText().toString().trim()).doubleValue() <= 0) {
							EmmApplication.T2("请填写首付金额");
							fundLoan_view.setText("");
							businessLoan_view.setText("");
						} else {
							if (s.toString().indexOf(".") >= 0) {
								if (s.toString().indexOf(".", s.toString().indexOf(".") + 1) > 0) {
									// 已重复输入
									fundLoan_view.setText(fundLoan_view.getText().toString()
											.substring(0, fundLoan_view.getText().toString().length() - 1));
									fundLoan_view.setSelection(fundLoan_view.getText().toString().length());
								}
							}
							String str = s.toString().trim();
							if (loantype_ == 1) {
								if (Double.valueOf(str).doubleValue() > (Double.valueOf(totalAmount_).doubleValue() - Double
										.valueOf(firstPay_view.getText().toString().trim()).doubleValue())) {
									Log.v(TAG, "totalAmount_:" + totalAmount_ + "-------" + str);
									handler.sendEmptyMessage(121);
									return;
								}
							} else if (loantype_ == 2) {
							} else if (loantype_ == 3) {
								Double number1 = Double.valueOf(str).doubleValue();
								Double total = Double.valueOf(totalAmount_).doubleValue()
										- Double.valueOf(firstPay_view.getText().toString().trim()).doubleValue();
								Double number2 = null;
								try {
									number2 = Double.valueOf(
											null == businessLoan_view.getText().toString().trim() ? "0"
													: businessLoan_view.getText().toString().trim()).doubleValue();
								} catch (NumberFormatException e) {
									number2 = 0.0;
								}
								if ((number1 + number2) > total) {
									handler.sendEmptyMessage(121);
									return;
								}
							}
						}
					}
				}
			}
		});
		businessLoan_view.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (null != s && !"".equals(s.toString())) {
					if (null == totalAmount_ || Double.valueOf(totalAmount_).doubleValue() <= 0) {
						EmmApplication.T2("房源总价不能为0");
						fundLoan_view.setText("");
						businessLoan_view.setText("");
					} else {
						if (null == firstPay_view.getText().toString().trim()
								|| "".equals(firstPay_view.getText().toString().trim())
								|| Double.valueOf(firstPay_view.getText().toString().trim()).doubleValue() <= 0) {
							EmmApplication.T2("请填写首付金额");
							fundLoan_view.setText("");
							businessLoan_view.setText("");
						} else {
							if (s.toString().indexOf(".") >= 0) {
								if (s.toString().indexOf(".", s.toString().indexOf(".") + 1) > 0) {
									// 已重复输入
									fundLoan_view.setText(fundLoan_view.getText().toString()
											.substring(0, fundLoan_view.getText().toString().length() - 1));
									fundLoan_view.setSelection(fundLoan_view.getText().toString().length());
								}
							}
							String str = s.toString().trim();
							if (loantype_ == 1) {
							} else if (loantype_ == 2) {
								if (Double.valueOf(str).doubleValue() > (Double.valueOf(totalAmount_).doubleValue() - Double
										.valueOf(firstPay_view.getText().toString().trim()).doubleValue())) {
									handler.sendEmptyMessage(121);
									return;
								}
							} else if (loantype_ == 3) {
								Double number1 = Double.valueOf(str).doubleValue();
								Double total = Double.valueOf(totalAmount_).doubleValue()
										- Double.valueOf(firstPay_view.getText().toString().trim()).doubleValue();
								Double number2 = null;
								try {
									number2 = Double.valueOf(
											null == fundLoan_view.getText().toString().trim() ? "0" : fundLoan_view
													.getText().toString().trim()).doubleValue();
								} catch (NumberFormatException e) {
									number2 = 0.0;
								}
								if ((number1 + number2) > total) {
									handler.sendEmptyMessage(121);
									return;
								}
							}
						}
					}
				}
			}
		});
		loanRate_view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(CalculatePriceActivity.this, FirstPayActivity.class);
				intent.putExtra("TYPE", "2");
				startActivity(intent);
			}
		});
		loanType_view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				listname = new ArrayList<String>();
				listname.add("公积金贷款");
				listname.add("商业贷款");
				listname.add("组合贷款");
				SetzpPopu(listname, "贷款类型", loanType_view);
			}
		});
		loanType_view.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (null != s) {
					if ("公积金贷款".equals(s.toString().trim())) {
						loantype_ = 1;
					} else if ("商业贷款".equals(s.toString().trim())) {
						loantype_ = 2;
					} else if ("组合贷款".equals(s.toString().trim())) {
						loantype_ = 3;
					} else {
						loantype_ = 0;
					}
					IsVsiBle();
				}
			}
		});
		loanLife_view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				listname2 = new ArrayList<InfoDest>();
				for (int i = 0; i < 30; i++) {
					InfoDest infoD = new InfoDest();
					infoD.setName((i + 1) + "年(" + (12 * (i + 1)) + "期)");
					infoD.setFid("" + (12 * (i + 1)));
					listname2.add(infoD);
				}
				SetzpPopu2(listname2, "贷款年限", loanLife_view);
			}
		});
		SetDropNumber2(housesSumPrice_view);
		SetDropNumber(firstPay_view);
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> edittext监听方法
	 * </p>
	 * <p>
	 * <b>Description：</b> 监听并处理多个小数点输入问题
	 * </p>
	 * 
	 * @param ed
	 * @author tangxin
	 * @date 2016年12月7日 上午11:45:08
	 */
	private void SetDropNumber2(final EditText ed) {
		ed.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (s.toString().indexOf(".") >= 0) {
					if (s.toString().indexOf(".", s.toString().indexOf(".") + 1) > 0) {
						// 已重复输入
						ed.setText(ed.getText().toString().substring(0, ed.getText().toString().length() - 1));
						ed.setSelection(ed.getText().toString().length());
					}
				}
				if (null != s) {
					totalAmount_ = s.toString().trim();
				}
			}
		});
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> edittext监听方法
	 * </p>
	 * <p>
	 * <b>Description：</b> 监听并处理多个小数点输入问题
	 * </p>
	 * 
	 * @param ed
	 * @author tangxin
	 * @date 2016年12月7日 上午11:45:08
	 */
	private void SetDropNumber(final EditText ed) {
		ed.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (s.toString().indexOf(".") >= 0) {
					if (s.toString().indexOf(".", s.toString().indexOf(".") + 1) > 0) {
						// 已重复输入
						ed.setText(ed.getText().toString().substring(0, ed.getText().toString().length() - 1));
						ed.setSelection(ed.getText().toString().length());
					}
				}
			}
		});
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 根据所选类型展示不同界面
	 * </p>
	 * <p>
	 * <b>Description：</b> 1="公积金贷款" 2="商业贷款" 3="组合贷款"
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 上午11:46:12
	 */
	private void IsVsiBle() {
		if (loantype_ == 1) {
			fundLoan_p1.setVisibility(View.VISIBLE);
			businessLoan_p1.setVisibility(View.GONE);
			fundLoan_p2.setVisibility(View.VISIBLE);
			businessLoan_p2.setVisibility(View.GONE);
		} else if (loantype_ == 2) {
			fundLoan_p1.setVisibility(View.GONE);
			businessLoan_p1.setVisibility(View.VISIBLE);
			fundLoan_p2.setVisibility(View.GONE);
			businessLoan_p2.setVisibility(View.VISIBLE);
		} else if (loantype_ == 3) {
			fundLoan_p1.setVisibility(View.VISIBLE);
			businessLoan_p1.setVisibility(View.VISIBLE);
			fundLoan_p2.setVisibility(View.VISIBLE);
			businessLoan_p2.setVisibility(View.VISIBLE);
		}
	}

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 121) {
				fundLoan_view.setText("0");
				businessLoan_view.setText("0");
				EmmApplication.T2("贷款金额不能高于未付金额");
			}
		};
	};

	GetCalculatePrice getcalculate;

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 算价接口
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @param map
	 * @author tangxin
	 * @date 2016年12月7日 上午11:47:09
	 */
	private void GetCalculatePrice(Map<String, String> map) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("params", map);
		JSONObject json = new JSONObject(paramsMap);
		String jsonstr = json.toString();
		RequestParams params = new RequestParams();
		params.put("body", jsonstr);
		Log.v("getCalculatePrice-->post", EmmApplication.urlStr + "?body=" + jsonstr);
		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.post(EmmApplication.urlStr, params, new AsyncHttpResponseHandler() {
			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束error:" + error + "==" + content);
			}

			@Override
			public void onFinish() {
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束");
				super.onFinish();
			}

			@Override
			public void onStart() {
				super.onStart();
				Loading.showDialogForLoading(CalculatePriceActivity.this, "正在加载...", false, true, 60000);
				Log.v(TAG, "请求开始");
			}

			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				if (content == null) {
					return;
				}
				Log.v(TAG, content);
				JSONObject jsonObj = JSONObject.parseObject(content);
				JSONObject resData = (JSONObject) jsonObj.get("resData");
				if (resData == null) {
					Log.v(TAG, "信息返回值为空");
					return;
				}

				getcalculate = JSONObject.parseObject(resData.toString(), GetCalculatePrice.class);

				if ("1".equals(getcalculate.getResult())) {
					Log.v("获取成功", "");
					EmmApplication.getcalculate = getcalculate;
					Intent intent = new Intent();
					intent.putExtra("pr_namestr", pr_namea);
					intent.putExtra("customerName", customerName_view.getText().toString().trim());
					intent.putExtra("customerPhone", customerPhone_view.getText().toString().trim());
					intent.putExtra("businessLoanRate", businessLoanRate_view.getText().toString().trim());
					intent.putExtra("fundLoanRate", fundLoanRate_view.getText().toString().trim());
					intent.putExtra("Rate", Rate);
					intent.putExtra("loantype_", loantype_);
					intent.setClass(CalculatePriceActivity.this, EndPriceActivity.class);
					startActivity(intent);
				} else {
					EmmApplication.T(getcalculate.getErrorMsg().toString());
					Log.v("获取失败", "errorCode:" + getcalculate.getErrorCode().toString() + "errorMsg:"
							+ getcalculate.getErrorMsg().toString());
				}
			}
		});
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 选项框
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @param listname
	 * @param title
	 * @param ed
	 * @author tangxin
	 * @date 2016年12月7日 上午11:47:38
	 */
	public void SetzpPopu(final List<String> listname, String title, final EditText ed) {
		view = LayoutInflater.from(CalculatePriceActivity.this).inflate(R.layout.popu_zp, null);
		popu = new PopupWindow(view, android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT,
				android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT);
		popu.setFocusable(true);
		// EmmApplication.setNumberLog(CalculatePriceActivity.this, 0.5f);//
		// 改变popupwindow范围外的屏幕亮度
		popu.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
		popu.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		popu.showAtLocation(view, Gravity.BOTTOM, 0, 0);
		popu.showAsDropDown(view, 0, 0);
		popu.setFocusable(true);
		popu.setOutsideTouchable(true);
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					popu.dismiss();
					EmmApplication.setLog(CalculatePriceActivity.this);
				}
				return false;
			}
		});
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				popu.dismiss();
				EmmApplication.setLog(CalculatePriceActivity.this);
			}
		});

		PickerView select_pick = (PickerView) view.findViewById(R.id.select_pick);

		selectName = listname.get(0);

		TextView shuerbtn = (TextView) view.findViewById(R.id.shuerbtn);
		RelativeLayout relative_all = (RelativeLayout) view.findViewById(R.id.relative_all);
		ImageView dis_btn = (ImageView) view.findViewById(R.id.dis_btn);
		dis_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				popu.dismiss();
				EmmApplication.setLog(CalculatePriceActivity.this);
			}
		});
		TextView title_select = (TextView) view.findViewById(R.id.title_select);

		title_select.setText(title);

		select_pick.setData(listname);
		select_pick.setOnSelectListener(new onSelectListener() {
			@Override
			public void onSelect(String text, int id) {
				selectName = text;
				SelectID = id;
			}
		});
		select_pick.setSelected(0);
		shuerbtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 确定
				popu.dismiss();
				EmmApplication.setLog(CalculatePriceActivity.this);
				ed.setText(selectName);
			}
		});
		relative_all.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});
	}

	private String fidg = "";

	/** 选项框 */
	public void SetzpPopu2(final ArrayList<InfoDest> listname, String title, final EditText ed) {
		view = LayoutInflater.from(CalculatePriceActivity.this).inflate(R.layout.popu_zp, null);
		popu = new PopupWindow(view, android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT,
				android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT);
		popu.setFocusable(true);
		popu.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		popu.showAtLocation(view, Gravity.BOTTOM, 0, 0);
		popu.showAsDropDown(view, 0, 0);
		popu.setFocusable(true);
		popu.setOutsideTouchable(true);
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					popu.dismiss();
					EmmApplication.setLog(CalculatePriceActivity.this);
				}
				return false;
			}
		});
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				popu.dismiss();
				EmmApplication.setLog(CalculatePriceActivity.this);
			}
		});

		PickerView select_pick = (PickerView) view.findViewById(R.id.select_pick);

		selectName = listname.get(0).getName();

		TextView shuerbtn = (TextView) view.findViewById(R.id.shuerbtn);
		RelativeLayout relative_all = (RelativeLayout) view.findViewById(R.id.relative_all);
		ImageView dis_btn = (ImageView) view.findViewById(R.id.dis_btn);
		dis_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				popu.dismiss();
				EmmApplication.setLog(CalculatePriceActivity.this);
			}
		});
		TextView title_select = (TextView) view.findViewById(R.id.title_select);

		title_select.setText(title);

		List<String> listnames = new ArrayList<String>();
		for (int i = 0; i < listname.size(); i++) {
			listnames.add(listname.get(i).getName());
		}
		select_pick.setData(listnames);
		select_pick.setOnSelectListener(new onSelectListener() {
			@Override
			public void onSelect(String text, int id) {
				selectName = text;
				SelectID = id;
			}
		});
		select_pick.setSelected(0);
		shuerbtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 确定
				popu.dismiss();
				EmmApplication.setLog(CalculatePriceActivity.this);
				ed.setText(selectName);

				for (int i = 0; i < listname.size(); i++) {
					if (selectName.equals(listname.get(i).getName())) {
						fidg = listname.get(i).getFid();
					}
				}

			}
		});
		relative_all.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});
	}

	public View view3;
	public PopupWindow popu_call;

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 上下文对象 提示内容 是否展示取消按钮
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @param msg
	 * @param map
	 * @author tangxin
	 * @date 2016年12月7日 上午11:47:54
	 */
	public void popu(String msg, final Map<String, String> map) {
		// , boolean isVscancel
		view3 = LayoutInflater.from(CalculatePriceActivity.this).inflate(R.layout.popu_shuerdp, null);
		popu_call = new PopupWindow(view3, android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT,
				android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT);
		popu_call.setFocusable(true);

		popu_call.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

		popu_call.showAtLocation(view3, Gravity.CENTER, 0, 0);
		popu_call.showAsDropDown(view3, 0, 0);
		popu_call.setFocusable(true);
		popu_call.setOutsideTouchable(true);
		view3.setFocusable(true);
		view3.setFocusableInTouchMode(true);

		view3.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
				}
				return false;
			}
		});
		// 取消
		TextView dismiss = (TextView) view3.findViewById(R.id.dismiss);
		// 确定
		TextView shuerbtn = (TextView) view3.findViewById(R.id.shuerbtn);

		dismiss.setText("否");
		shuerbtn.setText("是");

		TextView ts_message = (TextView) view3.findViewById(R.id.ts_message);
		ts_message.setText(msg);
		dismiss.setVisibility(View.VISIBLE);
		dismiss.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				popu_call.dismiss();
				EmmApplication.setLog(CalculatePriceActivity.this);
				GetCalculatePrice(map);
			}
		});
		shuerbtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				popu_call.dismiss();
				map.put("customerId", customerId);
				GetCalculatePrice(map);
				EmmApplication.setLog(CalculatePriceActivity.this);
			}
		});
	}

}
